//
//  BaseNavViewController.m
//  wensheng-sijicaifu
//
//  Created by kakarot on 2017/5/23.
//  Copyright © 2017年 kakarot. All rights reserved.
//

#import "BaseNavViewController.h"
#import "FadePushTransition.h"
@interface BaseNavViewController ()<UIGestureRecognizerDelegate,UINavigationControllerDelegate>
@property (nonatomic, copy) NSArray *blacklist;
@end

@implementation BaseNavViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    self.navigationBar.translucent = YES;
    //去掉分割线
//    self.navigationBar.barStyle = UIBarStyleBlack;
    [self.navigationBar setShadowImage:[UIImage new]];
    [self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
}


- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.childViewControllers.count > 0) {
        // 如果push进来的不是第一个控制器
        UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(MBWidthScale(5), 0, 0, MBHeightScale(50))];
        [backBtn setImage:[UIImage imageNamed:@"back-1"] forState:UIControlStateNormal];
        
        [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        
        //可以侧滑返回,但是上面自定义的返回按钮编程系统自带的了
//        viewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
        //自定义的返回按钮生效了, 但是无法侧滑返回了
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
        
        
        viewController.hidesBottomBarWhenPushed = YES; // 隐藏底部的工具条
    }
    /*一旦调用super的pushViewController方就会创建子控制器viewController的view并调用viewController的viewDidLoad方法。可以在viewDidLoad方法中重新设置自己想要的左上角按钮样式*/
    //在push一个新的VC时，禁用滑动返回手势
        if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.interactivePopGestureRecognizer.enabled = NO;
        }
//        [super pushViewController:viewController animated:animated];
    [super pushViewController:viewController animated:animated];
}

- (void)back{
    [self popViewControllerAnimated:YES];
}

///MARK: UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    //完全展示出VC时，启用滑动返回手势
    NSArray *VcArr = self.viewControllers;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = self;
        self.interactivePopGestureRecognizer.enabled = YES;
    }
    
//    if ([viewController isKindOfClass:[RVRegistViewController class]] && ![WSFirstLoad account].isFirstLoad)
//    {
//        //黑名单中的控制器关闭手势返回
//        navigationController.interactivePopGestureRecognizer.enabled = NO;
//        navigationController.interactivePopGestureRecognizer.delegate = nil;
//    }
    
    
    
    //解决根试图左滑页面卡死
    if (navigationController.viewControllers.count == 1) {
        navigationController.interactivePopGestureRecognizer.enabled = NO;
        navigationController.interactivePopGestureRecognizer.delegate = nil;
    }
}

///MARK: UIGestureRecognizerDelegate
//接受多手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
}

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{
    
        // 就是在这里判断是哪种动画类型
        // 也可以判断是哪一个viewcongroller
        if (self.isNewPush){
            return [FadePushTransition new]; // 返回push动画的类
        }else{
            return nil;
        }
    
}

@end
